package ios.di.logviewer.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 日期工具，用于Date和String之间的相互转换
 * @author LuoHouQi
 *
 */
public class DateUtil {
	public static final String PATTERN_1 = "yyyy/MM/dd HH:mm:ss";
	public static final String PATTERN_2 = "yyyy-MM-dd";
	private String datePattern;
	
	private DateUtil(String datePattern) {
		super();
		this.datePattern = datePattern;
	}
	public static DateUtil getDateUtil(String DATE_PATTERN)
	{
		return new DateUtil( DATE_PATTERN);
	}
	
	public  boolean validate(String date)
	{
		try {
			new SimpleDateFormat().parse(date);
		} catch (ParseException e) {
			return false;
		}
		return true;
	}
	
	public  Date parseDate(String date) throws ParseException
	{
		date = date.trim();
		SimpleDateFormat df = new SimpleDateFormat();
		df.applyPattern(datePattern);
		if(date==null || date.equals(""))
			return null;
		
			Date dateObj = df.parse(date);
			
			return dateObj;		
	}
	
	public  String convertToString(Date date )
	{
		if(date==null)
			return null;
		SimpleDateFormat df = new SimpleDateFormat();
		df.applyPattern(datePattern);
		return df.format(date);
		
	}
}
